package com.zhibang.sinocare.dict.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhibang.sinocare.common.domian.MedicalReport;
import com.zhibang.sinocare.common.domian.vo.MedicalReportVo;
import com.zhibang.sinocare.common.result.Result;
import com.zhibang.sinocare.common.result.ResultCode;
import com.zhibang.sinocare.dict.service.impl.MedicalReportServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/MedicalReport")
@Api(tags = "数据初始化模块" , description = "对页面数据初始化数据")
@CrossOrigin
public class MedicalReportController {
    @Resource
    private MedicalReportServiceImpl medicalReportServiceImpl;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param searchParams
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "find" ,method = RequestMethod.GET)
    @ApiOperation(value = "查询检测项初始数据表数据" ,notes = "根据条件查询数据" , httpMethod = "GET")
    public Result medicalReportFind(@RequestParam(value = "page",defaultValue = "1")  Integer page ,@RequestParam(value = "limit",defaultValue = "5") Integer limit ,String searchParams) throws JsonProcessingException {
        System.out.println("进入find");
        System.out.println(searchParams);

        MedicalReport medicalReport1 = null;
        ObjectMapper objectMapper = new ObjectMapper();

        if(searchParams != null){
             medicalReport1 = objectMapper.readValue(searchParams, MedicalReport.class);
        }

        Page page1 = PageHelper.startPage(page , limit);
        List<MedicalReport> medicalReports = medicalReportServiceImpl.queryBy(medicalReport1);

        for (MedicalReport medicalReport : medicalReports) {
            medicalReport.setReference(medicalReport.getReferenceLow()+"-"+medicalReport.getReferenceHigh());
        }
        return Result.ok(ResultCode.SUCCESS, page1.getTotal(), medicalReports);
    }


    /**
     * 添加和修改
     * @param medicalReportVo
     * @return
     */
    @RequestMapping(value = "addOnUpdate" , method = RequestMethod.POST)
    @ApiOperation(value = "查询检测项初始数据表数据" ,notes = "根据条件查询数据" , httpMethod = "POST")
    public Result addOnUpdate(MedicalReportVo medicalReportVo){
        System.out.println("medicalReport："+medicalReportVo);
        int i = medicalReportServiceImpl.addOnUpdate(medicalReportVo);
        System.out.println("数据条数"+i);
        if(i>0){
            return Result.ok(ResultCode.SUCCESS,i);
        }else{
            return Result.ok(ResultCode.FAIL,null);
        }
    }
}
